package org.rcloud.io.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.rcloud.io.Utils.ResultVo;
import org.rcloud.io.mapper.TVideoMapper;
import org.rcloud.io.model.TVideo;
import org.rcloud.io.service.TVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: ink-feather
 * @Description:
 * @Date: 2019/6/3
 */
@Service
public class TVideoServiceImpl implements TVideoService {

    @Autowired
    private TVideoMapper tVideoMapper;

    @Override
    public ResultVo save(TVideo tVideo) {

        if (tVideo.equals("") || tVideo == null) {
            return ResultVo.setError("传入数据为空");
        } else {
            tVideo.setvDate(new Date());
            int i = tVideoMapper.insert(tVideo);
            if (i > 0) {
                return ResultVo.setOK("添加成功");
            } else {
                return ResultVo.setError("添加失败");
            }
        }
    }

    @Override
    public ResultVo delete(Integer id) {
        int i = tVideoMapper.deleteByPrimaryKey(id);
        if (i > 0) {
            return ResultVo.setOK("删除成功");
        } else {
            return ResultVo.setError("删除失败");
        }
    }

    @Override
    public ResultVo update(TVideo tVideo) {
        if (tVideo.equals("") || tVideo == null) {
            return ResultVo.setError("传入数据为空");
        } else {
            int i = tVideoMapper.updateByPrimaryKeySelective(tVideo);
            if (i > 0) {
                return ResultVo.setOK("修改成功");
            } else {
                return ResultVo.setError("修改失败");
            }
        }
    }

    @Override
    public ResultVo selectById(Integer id) {
        try {
            TVideo tVideo = tVideoMapper.selectByPrimaryKey(id);
            return ResultVo.setOK(tVideo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.setError(e.getMessage());
        }
    }

    @Override
    public ResultVo selectAll(Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TVideo> list = tVideoMapper.selectAll();
        PageInfo<TVideo> pageInfo = new PageInfo<>(list);
        return ResultVo.setOK(pageInfo);
    }

    @Override
    public ResultVo selectByLike(Integer pageNum,Integer pageSize,String vName) {
        PageHelper.startPage(pageNum,pageSize);
        String s = "%" + vName + "%";
        List<TVideo> list = tVideoMapper.selectByLike(s);
        PageInfo<TVideo> pageInfo = new PageInfo<>(list);
        return ResultVo.setOK(pageInfo);
    }
}
